#!/usr/bin/env python

# Include the PySFML extension
from PySFML import sf

# Create the main window
window = sf.RenderWindow(sf.VideoMode(800, 600), "PySFML test")
window.SetPosition(sf.VideoMode.GetDesktopMode().Width/2-400, sf.VideoMode.GetDesktopMode().Height/2-300)

# Load image
image = sf.Image ()
image.LoadFromFile("../gfx/ship.png")
image.SetSmooth(True)

# Timing
clock = sf.Clock ()
clock2 = sf.Clock ()

# Make a sprite
sprite = sf.Sprite(image)
#sprite.SetPosition (100, 100)
sprite.SetPosition(window.GetWidth()/2, window.GetHeight()/2)
sprite.SetCenter(image.GetWidth()/2, image.GetHeight()/2)

turn = 0
frame = 0
frame2 = 0
clock.Reset()
clock2.Reset()

# Start the game loop
running = True
while running:
	event = sf.Event()

	while window.GetEvent(event):
		if event.Type == sf.Event.Closed:
			running = False

		if event.Type == sf.Event.KeyPressed:
			if event.Key.Code == sf.Key.Escape:
				running = False
			if event.Key.Code == sf.Key.Left or event.Key.Code == sf.Key.A:
				turn = 50
			if event.Key.Code == sf.Key.Right or event.Key.Code == sf.Key.D:
				turn = -50
		
		if event.Type == sf.Event.KeyReleased:
			if event.Key.Code == sf.Key.Left:
				turn = 0
			if event.Key.Code == sf.Key.Right:
				turn = 0
			if event.Key.Code == sf.Key.A:
				turn = 0
			if event.Key.Code == sf.Key.D:
				turn = 0

	sprite.Rotate(turn * window.GetFrameTime())
	
	# Clear screen, draw the text, and update the window
	window.Clear()
	window.Draw(sprite)
	window.Display()
	frame += 1
	frame2 += 1
	if clock.GetElapsedTime() > 1:
		print "frames per Second: %i" % frame
		print "\nTotal Time: %f - Total Frames: %i" % (clock2.GetElapsedTime(), frame2)
		frame = 0
		clock.Reset()
	

window.Close()
